package com.tencent.ied.bk.filter;

import com.tencent.ied.bk.entity.BaseAbstractInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chen_yu
 * @date 2021/3/11 3:17
 */
@Slf4j
public class FilterChain {

    public List<Filter> filters = new ArrayList<>();

    /**
     * 添加过滤器链
     *
     * @param f 过滤器
     * @return FilterChain
     */
    public FilterChain addFilter(Filter f) {
        filters.add(f);
        return this;
    }

    /**
     * 责任链调用
     *
     * @param baseAbstractInfo 基本摘要信息
     * @return
     */
    public boolean doFilter(BaseAbstractInfo baseAbstractInfo) {
        for (Filter filter : filters) {
            if (!filter.doFilter(baseAbstractInfo)) {
                log.info("{} 过滤不成功 return false", filter.getName());
                return false;
            }
//            log.info("{} 过滤成功 return true", filter.getName());
        }
        return true;
    }
}
